package com.skh.linkedlist;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class ListNode {
    int val;
    ListNode next;

    @Override
    public String toString() {
        List<Integer> result = new ArrayList<>();
        result.add(val);
        ListNode temp = next;
        while (temp != null) {
            result.add(temp.val);
            temp = temp.next;
        }
        return result.toString();
    }

    ListNode(int x) {
        this.val = x;
    }


    public static ListNode createListNode(String value) {
        //1,2,3
        // temp->cur(1)->2->3
        ListNode head = new ListNode(0);
        ListNode tempNode = head;
        String[] split = value.split(",");
        int[] values = Stream.of(split).mapToInt(Integer::parseInt).toArray();
        for (int val : values) {
            ListNode node = new ListNode(val);
            tempNode.next = node;
            tempNode = tempNode.next;
        }
        return head.next;
    }

    public static String printListNode(ListNode listNode) {
        List<Integer> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        while (listNode != null) {
            result.add(listNode.val);
            sb.append(listNode.val);
            if (listNode.next != null) {
                sb.append("->");
            }
            listNode = listNode.next;
        }
        System.out.println("result = " + result);
        System.out.println("链表 = " + sb.toString());
        return result.toString();
    }

    public static void main(String[] args) {
        ListNode listNode = ListNode.createListNode("3,2,54,13,4,1,4,6,8");
        ListNode.printListNode(listNode);
    }

}